#!/bin/sh

revision="backupplus v0.2.0 (2023-01-17 MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
	echo -ne "\n$ME-$thisname---->\n" >> $LOG
fi

if [ -f $TMP/backup.mib ] || [ -f $TMP/reboot.mib ] || [ -f $TMP/flash.mib ]; then
	echo "Backup or reboot is already running..."
	return 2> /dev/null
fi

OUTDIRA="$BACKUPFOLDER/advanced/$MUVERSION"
OUTDIR="$BACKUPFOLDER/advanced/$MUVERSION-FW-images"

case $1 in

-fileinfo) {

trap '' 2

	OUTPUT="fileinfo"
	#fileinfo
	echo -ne $OUTPUT"............\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIRA-fileinfo ]; then
		echo "Searching and storing *.fileinfo files now"
		on -f mmx find /mnt -name *.fileinfo -exec cp -c {} $OUTDIRA-fileinfo/ \; 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-special) {

trap '' 2

	OUTPUT="special"
	#export of special folders
		echo -ne $OUTPUT"............\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIRA-$OUTPUT"/" ]; then
		echo "dumping /net/rcc/mnt/efs-system/ now"
		on -f mmx cp -cr /net/rcc/mnt/efs-system/* $OUTDIRA-$OUTPUT/rcc/mnt/efs-system/ 2>> $LOG
		echo "dumping /net/rcc/mnt/efs-persist/ now"
		on -f mmx cp -cr /net/rcc/mnt/efs-persist/* $OUTDIRA-$OUTPUT/rcc/mnt/efs-persist/ 2>> $LOG
		echo "dumping /net/mmx/mnt/persist/ now"
		on -f mmx cp -cr /net/mmx/mnt/persist/* $OUTDIRA-$OUTPUT/mmx/mnt/persist/ 2>> $LOG
		on -f mmx cp -cr /net/mmx/dev/nvsku/serial_num $OUTDIRA-$OUTPUT/ 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-adb) {

trap '' 2

	OUTPUT="adb"
	#addressbookdb
	SEIZE="$(df -h /net/mmx/mnt/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"............\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIRA-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		on -f mmx cp -cr /net/mmx/mnt/$OUTPUT/* $OUTDIRA-$OUTPUT/ 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-app) {

trap '' 2

	OUTPUT="app.img"
	SEIZE="$(df -h /net/mmx/dev/mnand0t177 | awk '{print $2}')"
	echo -ne $OUTPUT"........\n" | $TEE -i -a $LOG
	if [ ! -f $OUTDIR/mmx2/app/70/default/app.img ]; then
		echo "dumping app.img now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		mkdir -p $OUTDIR/MMX2/app/70/default/
		on -f mmx cat /dev/mnand0t177 > $OUTDIR/MMX2/app/70/default/app.img 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-appf) { #app.img dump on file/directory base

trap '' 2

	OUTPUT="appimg"
	SEIZE="$(df -h /net/mmx/dev/mnand0t177 | awk '{print $2}')"
	echo -ne $OUTPUT"........\n" | $TEE -i -a $LOG
	if [ ! -f $OUTDIRA-$OUTPUT ]; then
		echo "copying app.img content now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		on -f mmx cp -cr /net/mmx/mnt/app/* $OUTDIRA-$OUTPUT/ 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-speech) {

trap '' 2

	OUTPUT="speech"
	SEIZE="$(df -h /net/mmx/mnt/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"..........\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIR-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		on -f mmx cp -cr /net/mmx/mnt/speech/tts/data_EU/ $OUTDIR/SpeechRes/speech-tts-data_eu/0/default/ 2>> $LOG
		on -f mmx cp -cr /net/mmx/mnt/speech/tts/data_ROW/ $OUTDIR/SpeechRes/speech-tts-data_row/0/default/ 2>> $LOG
		on -f mmx cp -cr /net/mmx/mnt/speech/tts/voices/ $OUTDIR/SpeechRes/speech-tts-voices/0/default/ 2>> $LOG

		on -f mmx cp -cr /net/mmx/mnt/speech/tts/vg/ $OUTDIR/SpeechAppRes/speech-tts-app-data-vg/0/default/ 2>> $LOG
		on -f mmx cp -cr /net/mmx/mnt/speech/common/ $OUTDIR/SpeechAppRes/speech-common-data/0/default/ 2>> $LOG
		on -f mmx cp -cr /net/mmx/mnt/speech/sr/data/ $OUTDIR/SpeechAppRes/speech-sr-data/0/default/ 2>> $LOG

		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-fwdump) {

trap '' 2

		on -f mmx cp -cr /net/rcc/mnt/efs-persist/tnrref.csv $OUTDIR/MuTnrRef/data/0/default/ 2>> $LOG
		on -f mmx cp -cr /net/rcc/mnt/efs-persist/SWDL/MainUnit-version2.txt $OUTDIR/MUConsistency/data/0/default/MainUnit-version2.txt 2>> $LOG
		
		touch $OUTDIR/"NOT A FW UPDATE" 2>> $LOG
		touch $OUTDIR/"BACKUP of FW main components" 2>> $LOG

ERROR

trap 2

return 2> /dev/null

};;

-media) {

trap '' 2

	OUTPUT="media"
	# music, pictures and videos stored on unit
	SEIZE="$(df -h /net/mmx/mnt/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"..........\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIRA-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		on -f mmx cp -cr /net/mmx/mnt/$OUTPUT/* $OUTDIRA-$OUTPUT/ 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-gndb) {

trap '' 2

	OUTPUT="gracenotedb"
	SEIZE="$(df -h /net/mmx/mnt/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"....\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIR-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		on -f mmx cp -cr /net/mmx/mnt/$OUTPUT/database/ $OUTDIR/Gracenote2/Database/0/default/ 2>> $LOG
		on -f mmx cp -cr /net/mmx/mnt/$OUTPUT/Update.txt $OUTDIR/Gracenote2/InfoFile/0/default/ 2>> $LOG
		on -f mmx cp -cr /net/mmx/mnt/$OUTPUT/config/gracenote.json $OUTDIR/Gracenote2/Version/0/default/ 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-bb) {

trap '' 2

	OUTPUT="boardbook"
	SEIZE="$(df -h /net/mmx/mnt/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"......\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIRA-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		if [ -f /net/mmx/mnt/$OUTPUT/boardbook*.iso ]; then
			on -f mmx cp -c -r -l0 /net/mmx/mnt/$OUTPUT/* $OUTDIRA-$OUTPUT/ 2>> $LOG
		else
			echo -ne "No boardbook on unit\n" | $TEE -i -a $LOG
		fi
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-RSDB) {

trap '' 2

	OUTPUT="RSDB"
	SEIZE="$(df -h /net/mmx/mnt/boardbook/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"......\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIR-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/boardbook/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		if [ ! -d /net/mmx/mnt/$OUTPUT/RSDB/ ]; then
			on -f mmx cp -cr /net/mmx/mnt/boardbook/$OUTPUT/VW_STL_DB.sqlite $OUTDIR/RadioStationDB/data/0/default/stl.1vw 2>> $LOG
			on -f mmx cp -cr /net/mmx/mnt/boardbook/$OUTPUT/Update.txt $OUTDIR/RadioStationDB/InfoFile/0/default/update.txt 2>> $LOG
		else
			echo -ne "No RSDB on unit\n" | $TEE -i -a $LOG
		fi
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-nav) {

trap '' 2

	OUTPUT="navdb"
	SEIZE="$(df -h /net/mmx/mnt/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"..........\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIRA-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		on -f mmx cp -cr /net/mmx/mnt/$OUTPUT/* $OUTDIRA-$OUTPUT/ 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;

-system) {

trap '' 2

	OUTPUT="system"
	SEIZE="$(df -h /net/mmx/mnt/$OUTPUT | awk '{print $3}')"
	echo -ne $OUTPUT"......\n" | $TEE -i -a $LOG
	if [ ! -d $OUTDIRA-$OUTPUT ]; then
		echo "dumping /net/mmx/mnt/"$OUTPUT"/ now"
		echo "duration: ~5min/GB"
		echo $SEIZE"B data"
		echo "please wait..."
		echo "there will be no additional output until you see - All done - again"
		on -f mmx cp -cr /net/mmx/mnt/$OUTPUT/* $OUTDIRA-$OUTPUT/ 2>> $LOG
		ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi
	
trap 2

return 2> /dev/null

};;


-rcc) {

trap '' 2

if [ -f $BACKUPFOLDER/$MUVERSION-RCC_fs0.bin ]; then
	RCC=$BACKUPFOLDER/$MUVERSION-RCC_fs0.bin
	echo -ne "Use dumped RCC from SD backup\n" | $TEE -i -a $LOG
else
	RCC="/net/rcc/dev/fs0"
	echo -ne "Dump from "$RCC"\n" | $TEE -i -a $LOG
fi

	echo "dumping RCC images now"
	echo "duration: max. 5 minutes"
	echo "please wait..."

	OUTPUT="RCC/ipl/21/default/"
	mkdir -p $OUTDIR/$OUTPUT
	echo -ne $OUTPUT".........." | $TEE -i -a $LOG
	if [ ! -f $OUTDIR/$OUTPUT"ipl-mib2.bin" ]; then
		SKIPBLOCKS="0"
		COUNTBLOCKS="32"
		on -f mmx $DD bs=4096 skip=$SKIPBLOCKS if=$RCC count=$COUNTBLOCKS of=$OUTDIR/$OUTPUT"ipl-mib2.bin" 2>> $LOG
	ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi

	OUTPUT="RCC/ifs-emg/21/default/"
	mkdir -p $OUTDIR/$OUTPUT
	echo -ne $OUTPUT"....." | $TEE -i -a $LOG
	if [ ! -f $OUTDIR/$OUTPUT"ifs-emergency.ifs" ]; then
		SKIPBLOCKS="32"
		COUNTBLOCKS="1312"
		on -f mmx $DD bs=4096 skip=$SKIPBLOCKS if=$RCC count=$COUNTBLOCKS of=$OUTDIR/$OUTPUT"ifs-emergency.ifs" 2>> $LOG
	ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi

	OUTPUT="RCC/ifs-root/21/default/"
	mkdir -p $OUTDIR/$OUTPUT
	echo -ne $OUTPUT".........." | $TEE -i -a $LOG
	if [ ! -f $OUTDIR/$OUTPUT"ifs-root.ifs" ]; then
		SKIPBLOCKS="1344"
		COUNTBLOCKS="6144"
		on -f mmx $DD bs=4096 skip=$SKIPBLOCKS if=$RCC count=$COUNTBLOCKS of=$OUTDIR/$OUTPUT"ifs-root.ifs" 2>> $LOG
	ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi

	OUTPUT="RCC/efs-system/21/default/"
	mkdir -p $OUTDIR/$OUTPUT
	echo -ne $OUTPUT"........" | $TEE -i -a $LOG
	if [ ! -f $OUTDIR/$OUTPUT"efs-system.efs" ]; then
		SKIPBLOCKS="7488"
		COUNTBLOCKS="8128"
		on -f mmx $DD bs=4096 skip=$SKIPBLOCKS if=$RCC count=$COUNTBLOCKS of=$OUTDIR/$OUTPUT"efs-system.efs" 2>> $LOG
	ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi

	OUTPUT="RCC/dsp/21/default/"
	mkdir -p $OUTDIR/$OUTPUT
	echo -ne $OUTPUT".." | $TEE -i -a $LOG
	if [ ! -f $OUTDIR/$OUTPUT"AUDI_MIB_DSP.bin.bgz" ]; then
		SKIPBLOCKS="15616"
		COUNTBLOCKS="768"
		on -f mmx $DD bs=4096 skip=$SKIPBLOCKS if=$RCC count=$COUNTBLOCKS of=$OUTDIR/$OUTPUT"AUDI_MIB_DSP.bin.bgz" 2>> $LOG
	ERROR
	else
		echo -ne " -> Backup is already there!\n" | $TEE -i -a $LOG
	fi

on -f mmx cp -cr  /net/rcc/etc/version/RCC-version.txt $OUTDIR/RCC/rcc_version_info.txt 2>> $LOG

trap 2

return 2> /dev/null

	};;
	
-mmx) {

trap '' 2

# TODO: NOT ready
# check MHI2Q

MMX="/net/mmx/dev/fs0"

echo -ne "MXX export not supported yet\n"

mkdir -p $OUTDIR/MMX2/ 2>> $LOG
touch $OUTDIR/MMX2/"MMX dump not fully implemented yet" 2>> $LOG
head -1 /mnt/app/img_ver.txt | sed "s/^#//" > $OUTDIR/MMX2/img_ver.txt 2>> $LOG

ERROR

trap 2

return 2> /dev/null

	};;

# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION]"
		echo ""
		echo "Options:"
		echo "        -app 		dump app.img"
		echo "        -apps 		dump app.img as file structure"
		echo "        -rcc 		dump RCC images"
		echo "        -mmx 		dump MMX images"
		echo "        -abd 		dump AddressBookDB"
		echo "        -gndb 		dump GraceNoteDB"
		echo "        -bb 		dump boardbook"
		echo "        -RSDB 		dump RadioStationDB"
		echo "        -fileinfo	dump *.fileinfo files"
		echo "        -media 		dump Media"
		echo "        -nav 		dump NavDB"
		echo "        -system 	dump /mnt/system"
		echo "        -special 	dump special folders"
		echo "        --help		show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
		
	};;
	
	esac

exit 0

